﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.UI.HtmlControls;
using CoolERP_BLL;
using CoolERP_Entities;

namespace CoolERP_Web.User
{
    public partial class SCJH_Query : BasePage
    {
        private int iStatus
        {
            get { return this.ViewState["iStatus"] == null ? 0 : Convert.ToInt32(this.ViewState["iStatus"]); }
            set { this.ViewState["iStatus"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindData();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }

        protected void LinkButton_Command(object sender, CommandEventArgs e)
        {
            iStatus = Convert.ToInt32(e.CommandArgument);
            this.AspNetPager1.CurrentPageIndex = 1;
            GridView2.Columns[0].Visible = GridView2.Columns[6].Visible = iStatus == 1;
            BindData();
        }

        private void BindData()
        {
            int RecordCount = 0;
            Dictionary<string, string> dc = new Dictionary<string, string>();
            if (!string.IsNullOrEmpty(txtHTBH.Text.Trim()))
                dc.Add("HTBH", txtHTBH.Text.Trim());
            if (!string.IsNullOrEmpty(txtKHJC.Text.Trim()))
                dc.Add("KHJC", txtKHJC.Text.Trim());
            if (!string.IsNullOrEmpty(txtCPBH.Text.Trim()))
                dc.Add("CPBH", txtCPBH.Text.Trim());
            if (!string.IsNullOrEmpty(txtS_Time.Text.Trim()))
                dc.Add("sDate", txtS_Time.Text.Trim());
            if (!string.IsNullOrEmpty(txtE_Time.Text.Trim()))
                dc.Add("eDate", txtE_Time.Text.Trim());
            if (iStatus == 0)
            {
                GridView1.Visible = true;
                GridView2.Visible = false;
                using (XSHTMXBBLL xBLL = new XSHTMXBBLL())
                {
                    GridView1.DataSource = xBLL.GetSCJH_HTMXB(dc, "CPBH", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                    GridView1.DataBind();
                }
            }
            else
            {
                GridView1.Visible = false;
                GridView2.Visible = true;
                using (SCJHBLL sBLL = new SCJHBLL())
                {
                    int zt = iStatus == 1 ? 0 : 1;
                    GridView2.DataSource = sBLL.GetPagerList(t => t.JHZT == zt, "JHKSSJ DESC", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                    GridView2.DataBind();
                }
            }
            this.AspNetPager1.RecordCount = RecordCount;
            this.AspNetPager1.Visible = RecordCount > 0;
            panMsg.Visible = !this.AspNetPager1.Visible;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                SCJH entity = e.Row.DataItem as SCJH;
                ImageButton btnEdit = (ImageButton)(e.Row.FindControl("btnEdit"));
                ImageButton btnConfirm = (ImageButton)(e.Row.FindControl("btnConfirm"));
                Label lblZT = ((Label)(e.Row.FindControl("lblZT")));

                if (entity.JHZT == 0)
                {
                    lblZT.Text = "待确认";
                    lblZT.ForeColor = System.Drawing.Color.Red;
                    btnConfirm.Visible = true;
                    btnEdit.Visible = true;
                    btnEdit.PostBackUrl = string.Format("SCJH_Edit.aspx?id={0}", entity.ID);
                }
                else
                {
                    lblZT.ForeColor = System.Drawing.Color.Green;
                    lblZT.Text = "已确认";
                    btnConfirm.Visible = false;
                    btnEdit.Visible = false;
                }
            }
        }

        protected void GridView2_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Guid id = Guid.Parse(GridView1.DataKeys[e.NewSelectedIndex].Value.ToString());
            using (SCJHBLL sBLL = new SCJHBLL())
            {
                SCJH entity = sBLL.GetEntity(t => t.ID == id);
                entity.JHZT = 1;
                if (sBLL.Update(entity))
                    ShowMessage("确认成功");
                else
                    ShowMessage("确认失败");
            }
            BindData();
        }

        protected void btnDel_Click(object sender, ImageClickEventArgs e)
        {
            StringBuilder sbIds = new StringBuilder();
            foreach (GridViewRow container in GridView1.Rows)
            {
                HtmlInputCheckBox cBox = (HtmlInputCheckBox)container.FindControl("chkSel");
                if (cBox != null && cBox.Checked)
                    sbIds.Append(string.Format(",{0}", cBox.Value));
            }
            if (sbIds.Length > 0)
            {
                sbIds = sbIds.Remove(0, 1);
                using (SCJHBLL sBLL = new SCJHBLL())
                {
                    if (sBLL.Deletes("ID", sbIds.ToString()))
                        this.ShowMessage("删除成功");
                    else
                        this.ShowMessage("删除失败");
                }
                BindData();
            }
        }
    }
}